package com.song.tools.mvc.validator;

import cn.hutool.core.util.StrUtil;

import java.util.function.Supplier;

/**
 * 字符串非空校验器
 *
 * @author song tools
 * @since 2024-06-21
 */
public record StrNotBlankValidator(String paramName) implements ParamValidator<String> {

    /**
     * 执行参数校验
     *
     * @param paramValueSupplier 参数值supplier
     * @return 校验结果，包含是否通过及失败原因
     */
    @Override
    public ValidationResult validate(Supplier<String> paramValueSupplier) {
        String paramValue = paramValueSupplier.get();
        if (StrUtil.isBlank(paramValue)) {
            return new ValidationResult(false, getDefaultErrorMessage());
        }
        return new ValidationResult(true, null);
    }

    @Override
    public String getDefaultErrorMessage() {
        return paramName + " must not be blank";
    }
}
